package com.eqdd.yiqidian.ui.discover.gonggao;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.bean.rebuild.NoticeListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ListResultBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.ui.mine.pizhun.NewNoticeInfoActivity;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * @author吕志豪 .
 * @date 18-4-16  上午10:39.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 * 通知-公告-通告
 */
@DealCode(codes = {CodeRule.STAFF_TONGZHI, CodeRule.STAFF_GONGGAO, CodeRule.STAFF_TONGGAO})
public class AllNoticeListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<NoticeListResultBean> slimAdapterEx;
    private int pageNum = 0;

    @Subscribe(codes = {CodeRule.STAFF_TONGZHI}, threadMode = ThreadMode.MAIN)
    public void refreshTongzhi() {

    }

    @Subscribe(codes = {CodeRule.STAFF_GONGGAO}, threadMode = ThreadMode.MAIN)
    public void refreshGonggao() {

    }

    @Subscribe(codes = {CodeRule.STAFF_TONGGAO}, threadMode = ThreadMode.MAIN)
    public void refreshTonggao() {

    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "通知-公告-通告");
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_128, new SlimInjector<NoticeListResultBean>() {
            @Override
            public void onInject(NoticeListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_content, "【" + data.getName() + "】" + data.getTheme())
                        .text(R.id.tv_date, data.getCreateTime());
            }
        }).attachTo(dataBinding.recyclerView);

        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    NoticeListResultBean itemBean = slimAdapterEx.getData().get(position);
                    Intent intent = new Intent(AllNoticeListActivity.this, NewNoticeInfoActivity.class);
                    intent.putExtra("noticeId", itemBean.getId());
                    intent.putExtra("who", 1);
                    startActivity(intent);
                });


        ModelRx2DataSource<NoticeListResultBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResult<ListResultBean<NoticeListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.ALL_MY_NOTICE)
                        .params("page", pageNum)
                        .params("companyId", GreenDaoUtil.getUser().getCompanyId())
                        .params("departId", GreenDaoUtil.getUser().getDepartId())
                        .converter(new JsonConverter<HttpResult<ListResultBean<NoticeListResultBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<HttpResult<ListResultBean<NoticeListResultBean>>>())
                        .flatMap(new Function<HttpResult<ListResultBean<NoticeListResultBean>>, Publisher<List<NoticeListResultBean>>>() {
                            @Override
                            public Publisher<List<NoticeListResultBean>> apply(@NonNull HttpResult<ListResultBean<NoticeListResultBean>> noticeWraperResultBeanHttpResult) throws Exception {
                                pageNum = noticeWraperResultBeanHttpResult.getItems().getPage();
                                return Flowable.just(noticeWraperResultBeanHttpResult.getItems().getList() == null ? new ArrayList<NoticeListResultBean>() : noticeWraperResultBeanHttpResult.getItems().getList());
                            }
                        });

            }
        }, 10);

        slimAdapterEx.updateData(new ArrayList<>());
        MVCCoolHelper<List<NoticeListResultBean>> mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();


    }

    @Override
    public void setView() {

    }
}
